import checker from './js/check.js'

$('.js-get-yzm').click(function(){
    const $this = $(this);
    if($this.hasClass('js-disabled')){
        return;
    }

    const targetPhoneSelector = $this.attr('data-target-phone-number');
    var $targetPhoneInput = $(targetPhoneSelector);
    var phone =  $targetPhoneInput.val();
    if(!checker.isPhoneValid(phone)){
        $.toast('手机号格式错误');
        return false;
    }
    let count = $this.attr('data-count') || '60';
    count=count*1;

    var url = $this.attr('data-url');

    $.showPreloader('正在请求验证码...')
    $.ajax({
        url:url,
        data:{
            number:phone
        },
        success:function(){
            $.hidePreloader();
            $.alert('请求验证码成功')

            $this.addClass('js-disabled')
            let time=count;

            $this.text(`${time}秒后可重发`);
            time--;

            const interval = setInterval(function(){
                $this.text(`${time}秒后可重发`);
                time--;
                if(time<0){
                    clearInterval(interval)
                    $this.text('获取验证码').removeClass('js-disabled')
                }
            },1000)
        },
        error:function(){
            $.hidePreloader();
            $.toast('请求验证码错误，请重试');
        }
    })

})




$('body').on('click','#js-header-refresh',function(){
    window.location.reload(true)
})


$('a.external').click(function(){
    var $this = $(this)
    if($this.attr('href')){
        $.showPreloader('让请求数据飞一会');
    }
})

$('.js-24-hours').each(function(){
    var $this = $(this)
    var begin = 1,
        defaultHour = 12;
    if($this.hasClass('in')){
        begin = new Date().getHours()+1
        defaultHour = begin>18?begin:18
    }
    var optionsStr =''
    for(var i = begin;i<=24;i++){
        var ii = i<10?'0'+i:i
        optionsStr+=`<option value="${ii}" ${i==defaultHour?'selected':''}>${i}点</option>`
    }
    $this.html(optionsStr)
})